package senpec.qc.harmony.base.mybatisplus;

import lombok.Data;

import java.util.HashMap;
import java.util.Map;

@Data
public class Result<T> {
    private Boolean status;
    private String msg;
    /**
     * 类型，用于消息框的类型
     * success：返回成功的类型。
     * error：返回失败的类型。
     */
    private String type;
    private Map<String, Object> meta;
    private T body;

    public static <T> Result<T> ok() {
        Result<T> result = new Result<>();
        result.setStatus(true);
        result.setMsg("操作成功！");
        result.setType("success");
        result.setMeta(new HashMap<>());
        return result;
    }

    public static <T> Result<T> ok(T body) {
        Result<T> result = new Result<>();
        result.setStatus(true);
        result.setMsg("操作成功！");
        result.setType("success");
        result.setMeta(new HashMap<>());
        result.setBody(body);
        return result;
    }

    public static <T> Result<T> error() {
        Result<T> result = new Result<>();
        result.setStatus(false);
        result.setMsg("操作失败！");
        result.setType("error");
        result.setMeta(new HashMap<>());
        return result;
    }

    public static <T> Result<T> error(T body) {
        Result<T> result = new Result<>();
        result.setStatus(false);
        result.setMsg("操作失败！");
        result.setType("error");
        result.setMeta(new HashMap<>());
        result.setBody(body);
        return result;
    }


    public Result<T> msg(String msg) {
        this.msg = msg;
        return this;
    }

    public Result<T> body(T body) {
        this.body = body;
        return this;
    }

    public Result<T> putMeta(String key, Object value) {
        this.meta.put(key, value);
        return this;
    }

    public Result<T> putMetas(Map<String, Object> metas) {
        this.meta.putAll(metas);
        return this;
    }

    public Result<T> addSuccess(String msg) {
        this.msg = "新增" + msg + "成功！";
        return this;
    }

    public Result<T> addFail(String msg) {
        this.msg = "新增" + msg + "失败！";
        return this;
    }

    public Result<T> updateSuccess(String msg) {
        this.msg = "修改" + msg + "成功！";
        return this;
    }

    public Result<T> updateFail(String msg) {
        this.msg = "修改" + msg + "失败！";
        return this;
    }

    public Result<T> deleteSuccess(String msg) {
        this.msg = "删除" + msg + "成功！";
        return this;
    }

    public Result<T> deleteFail(String msg) {
        this.msg = "删除" + msg + "失败！";
        return this;
    }
}
